
*** Note this file uses the predictions from the Graded response IRT model estimates
** all the directories references may need to replace / with \ - sorry Windows users!!

**** FIRST COLLECT FIRST-WAVE LATENT ESTIMATES FOR EACH PANEL DATA RESPONDENT 
*** YOU NEED TO RUN TABLE 2 GRADED RESPONSE MODEL ESTIMATES FIRST to CREATE THIS - estimate_table2_figure3_siC.do
use data/gsem_grm_prepost_1952.dta

** 1956


*** For panel evaluations
keep if compareyear==1956
rename VCF0006 ID1956

keep ID1956 theta_grm_free partystrength_free theta_grm_fixed partystrength_fixed
sum ID1956
save "data/gsem_grm_prepost_mergewith1956.dta", replace

** 1972

clear
use "data/gsem_grm_prepost_1952.dta"

*** For panel evaluations
keep if compareyear==1972
rename VCF0006 ID1972

keep ID1972 theta_grm_free partystrength_free theta_grm_fixed partystrength_fixed
sum ID1972
save "data/gsem_grm_prepost_mergewith1972.dta", replace


** 1992

clear

use "data/gsem_grm_prepost_1952.dta"


*** For panel evaluations

keep if compareyear==1992
rename VCF0006 ID1992
keep if ID1992<2000
tab ID1992

keep ID1992 theta_grm_free partystrength_free theta_grm_fixed partystrength_fixed
sum ID1992
save "data/gsem_grm_prepost_mergewith1992.dta", replace


**2000

clear

use "data/gsem_grm_prepost_1952.dta"


*** For panel evaluations

keep if compareyear==2000
rename VCF0006 ID2000
tab ID2000

keep ID2000 theta_grm_free partystrength_free theta_grm_fixed partystrength_fixed
sum ID2000
save "data/gsem_grm_prepost_mergewith2000.dta", replace


**2016


clear

use "data/gsem_grm_prepost_1952.dta"


*** For panel evaluations

keep if compareyear==2016
rename VCF0006 V160001

keep V160001 theta_grm_free partystrength_free theta_grm_fixed partystrength_fixed
sum V160001
save "data/gsem_grm_prepost_mergewith2016.dta", replace


use "data/anes_timeseries_2016.dta"

merge 1:1 V160001 using data/gsem_grm_prepost_mergewith2016.dta
gen pid7 = V161158x
recode pid7 -9=. -8=.
drop _merge

merge 1:1 V160001_orig using data/anes2020reinterview_mergewith2016.dta

svyset [pweight=V200011a], strata(V200011d) psu(V200011c)

tab V201231x 
gen pid7_2020 = V201231x
recode pid7_2020 -9=.
svy: proportion pid7_2020

tab pid7_2020 pid7, col

gen pidstrength2016 = abs(pid7-4)
gen reptherm2016 = V161096
recode reptherm2016 -99=. -89=. -88=.
gen demtherm2016 = V161095
recode demtherm2016 -99=. -89=. -88=.
gen thermdiff = demtherm2016-reptherm2016

gen partythermstrength = abs(thermdiff)

gen pidswitch2020 = 0 if pid7!=. & pid7_2020!=.
replace pidswitch2020 = 1 if pid7>=1 & pid7<=3 & pid7_2020>=5 & pid7_2020<=7
replace pidswitch2020 = 1 if pid7>=5 & pid7<=7 & pid7_2020>=1 & pid7_2020<=3
svy: proportion pidswitch2020
svy: proportion pidswitch2020 if pid7!=4

gen pidswitchtest = 1-pidswitch2020
replace pidswitchtest = . if pidstrength2016==0

roctab pidswitchtest pidstrength

roctab pidswitchtest partythermstrength

roctab pidswitchtest partystrength_free

roccomp pidswitchtest pidstrength partystrength_free
roccomp pidswitchtest partythermstrength partystrength_free

sum partystrength_free, detail

gen tripart_latent = 0  if partystrength_free<=2
replace tripart_latent = 1 if partystrength_free>2 & partystrength_free<=5.79
replace tripart_latent = 2 if partystrength_free>5.79 & partystrength_free<10

sum partythermstrength, detail
gen tripart_therm = 0  if partythermstrength<=15
replace tripart_therm = 1 if partythermstrength>15 & partythermstrength<=60
replace tripart_therm = 2 if partythermstrength>60 & partythermstrength<=100


svy: tab pidswitchtest tripart_latent, col
svy: tab pidswitchtest pidstrength, col
svy: tab pidswitchtest tripart_therm, col

clear


*******************************************
*** NOW ESTIMATE AROC
*** THIS IS JUST FOR OBSERVED DATA
*******************************************

** 2000

clear
use "data/amelia00s.dta" 
merge 1:1 id using data/observed00s.dta, keepusing(pref00 pref04) 
drop _merge

merge 1:1 id using "data/id_panel2000.dta"
drop _merge
merge 1:1 ID2000 using data/gsem_grm_prepost_mergewith2000.dta


tab pid00 [iw=WT00PRE]
tab pid04 [iw=WT04]

**
sum theta_grm_fixed [iw=WT00PRE] if pid00==3
sum theta_grm_free  [iw=WT00PRE] if pid00==3

gen partyidswitch = 0 if pid00!=. & pid04!=.
replace partyidswitch = 1 if pid00<=2 & pid04>=4 & pid04<7
replace partyidswitch = 1 if pid00>=4 & pid00<7 & pid04<=2

** take out independents
replace partyidswitch = . if pid00==3
proportion partyidswitch [pw=WT04]


gen pidstrength00 = abs(pid00-3)
gen partythermstrength00 = abs(demtherm00 - reptherm00)

replace partyidswitch = 1-partyidswitch

roctab partyidswitch pidstrength00
roctab partyidswitch partythermstrength00 
roctab partyidswitch partystrength_free


** 1992

clear

use data/amelia90s.dta
merge 1:1 id using data/observed90s.dta, keepusing(pref92 pref96)
drop _merge
gen ID1992 = id-920000
merge 1:1 ID1992 using data/gsem_grm_prepost_mergewith1992.dta
drop if _merge!=3
drop _merge

tab pid92
tab pid96

gen partyidswitch = 0 if pid92!=. & pid96!=.
replace partyidswitch = 1 if pid92<=2 & pid96>=4 & pid96<7
replace partyidswitch = 1 if pid92>=4 & pid92<7 & pid96<=2
proportion partyidswitch [pw=weight]

** take out independents
replace partyidswitch = . if pid92==3
proportion partyidswitch [pw=weight]

gen pidstrength92 = abs(pid92-3)
gen partythermstrength92 = abs(demtherm92 - reptherm92)
replace partyidswitch = 1-partyidswitch

roctab partyidswitch pidstrength92
roctab partyidswitch partythermstrength92 
roctab partyidswitch partystrength_free


** 1972

clear

use data/amelia70s.dta
merge 1:1 id using data/observed70s.dta
drop _merge
merge 1:1 id using data/id_panel1972.dta
drop _merge

merge 1:1 ID1972 using data/gsem_grm_prepost_mergewith1972.dta
drop if _merge!=3
drop _merge

svyset PSU

** PID matches
svy: proportion pid72
svy: proportion pid76


tab pid72
tab pid76

gen partyidswitch = 0 if pid72!=. & pid76!=.
replace partyidswitch = 1 if pid72<=2 & pid76>=4 & pid76<7
replace partyidswitch = 1 if pid72>=4 & pid72<7 & pid76<=2
svy: proportion partyidswitch 

** take out independents
replace partyidswitch = . if pid72==3
svy: proportion partyidswitch 


gen pidstrength72 = abs(pid72-3)
gen partythermstrength72 = abs(demtherm72 - reptherm72)
replace partyidswitch = 1-partyidswitch

roctab partyidswitch pidstrength72
roctab partyidswitch partythermstrength72 
roctab partyidswitch partystrength_free

roccomp partyidswitch partystrength_free pidstrength72 
roccomp partyidswitch partystrength_free partythermstrength72


** 1956

clear
use data/anes_mergedfile_1956to1960.dta
gen id = _n
keep id V560171 V560172 V560002
merge 1:1 id using data/amelia50s.dta

tab female V560171
tab black V560172
drop _merge
keep if pre56==1
merge 1:1 id using data/observed50s.dta, keepusing(pref56 pref60 V560003) 

drop if _merge==2
drop _merge

rename V560002 ID1956
merge 1:1 ID1956 using data/gsem_grm_prepost_mergewith1956.dta
drop if _merge!=3
drop _merge

svyset V560003

svy: proportion pid56
svy: proportion pid60

corr pid56 theta_grm_*

gen partyidswitch = 0 if pid56!=. & pid60!=.
replace partyidswitch = 1 if pid56<=2 & pid60>=4 & pid60<7
replace partyidswitch = 1 if pid56>=4 & pid56<7 & pid60<=2
svy: proportion partyidswitch

** take out independents
replace partyidswitch = . if pid56==3
svy: proportion partyidswitch 

gen pidstrength56 = abs(pid56-3)
replace partyidswitch = 1-partyidswitch

roctab partyidswitch pidstrength56
roctab partyidswitch partystrength_free

roccomp partyidswitch partystrength_free pidstrength56
